#Includes what packages are libraries
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

library(tidyverse)
library(here)
library(sf)
library(tmap)
### Update packages(ask = FALSE)

Read in the data

sf_trees <- read_csv(here('data', 'sf_trees', 'sf_trees.csv'))

Part 1: Wrangling and ggplot review

Example 1: Find couunts of observation by legal_status & wrangle a bit.

### Method 1: group_by() %>%  summarize()

sf_trees %>% 
  group_by(legal_status) %>% #groups sf_trees by legal_status
  summarize(tree_count = n()) #outputs the count of each value in legal status
## # A tibble: 10 × 2
##    legal_status                 tree_count
##    <chr>                             <int>
##  1 DPW Maintained                   141725
##  2 Landmark tree                        42
##  3 Permitted Site                    39732
##  4 Planning Code 138.1 required        971
##  5 Private                             163
##  6 Property Tree                       316
##  7 Section 143                         230
##  8 Significant Tree                   1648
##  9 Undocumented                       8106
## 10 <NA>                                 54
### Method 2: different way plus a few new functions

top_5_status <- sf_trees %>% 
  count(legal_status) %>% #outputs count of each value and groups by legal_status
  drop_na(legal_status) %>%  #drops any na values in legal_status
  rename(tree_count = n) %>% 
  relocate(tree_count) %>%  #brings given column name to the front of the data set
  slice_max(tree_count, n = 5) %>%  #slices out the top 5 values in tree_count
  arrange(-tree_count) #arranges by tree_count descending (b/c of - sign) (can also do arrange(desc(tree_count))

Make a graph of the top 5 from above:

ggplot(data = top_5_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) +
  geom_col(fill = 'darkgreen') +
  labs(x = 'Legal status', y = 'Tree count') +
  coord_flip() + #switches columns to face horizontally 
  theme_minimal()

Example 4: use tidyr::separate()

sf_trees_sep <- sf_trees %>% 
  separate(species, into = c('spp_scientific', 'spp_common'), sep = ' :: ')

Example 5: use tidyr::unite()

ex_5 <- sf_trees %>% 
  unite('id_status', tree_id, legal_status, sep = '_COOL_')

Part 2: make some maps

Step 1: convert lat/lon to spatial point, st_as_sf()

blackwood_acacia_sf <- blackwood_acacia_df %>% 
  drop_na(lat, lon) %>% 
  st_as_sf(coords = c('lon', 'lat'))

### we need to tell R what the coordinate reference system is 

st_crs(blackwood_acacia_sf) <- 4326 # This is the EPSG number for the basic WGS84 lat and lon in degrees

ggplot(data = blackwood_acacia_sf) +
  geom_sf(color = 'darkgreen') + 
  theme_minimal()

Read in the SF shapefile and add to map

sf_map <- read_sf(here('data', 'sf_map', 'tl_2017_06075_roads.shp'))

sf_map_transform <- st_transform(sf_map, 4326)

ggplot(data = sf_map_transform) + 
  geom_sf()

Combine these maps:

ggplot() +
  geom_sf(data = sf_map, 
          size = 0.1,
          color = 'darkgrey') + 
  geom_sf(data = blackwood_acacia_sf, 
          color = 'red',
            size = 0.5) + 
  theme_void() + 
  labs(title = 'Blackwood acacias in SF')

Now an interactive map!

tmap_mode('view')

tm_shape(blackwood_acacia_sf) + 
  tm_dots()